<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
            <el-form-item label="预约人姓名" prop="name">
                <el-input v-model="queryParams.name" placeholder="请输入预约人姓名" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-if="refreshTable" :data="menuList" row-key="menuId" :default-expand-all="isExpandAll"
            :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
            <el-table-column prop="name" label="预约人姓名" ></el-table-column>
            <el-table-column prop="orderNum" label="预约人数"></el-table-column>
            <el-table-column prop="unit" label="预约单位"></el-table-column>
            <el-table-column prop="phoneNum" label="联系方式" ></el-table-column>
            <el-table-column prop="orderTime" label="预约时间" ></el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-edit"
                    @click="handleUpdate(scope.row)"
                >修改</el-button>
                <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-delete"
                    @click="handleDelete(scope.row)"
                >删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />
         <!-- 添加或修改菜单对话框 -->
         <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="100px">
                <el-form-item label="预约人姓名" prop="name">
                    <el-input type="text" v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="预约人数" prop="orderNum">
                    <el-input type="number" v-model="form.orderNum"></el-input>
                </el-form-item>
                <el-form-item label="预约单位" prop="unit">
                    <el-input type="text" v-model="form.unit"></el-input>
                </el-form-item>
                <el-form-item label="联系方式" prop="phoneNum">
                    <el-input type="text" v-model="form.phoneNum"></el-input>
                </el-form-item>
                <el-form-item label="预约日期" prop="date">
                    <el-date-picker
                        style="width: 100%;"
                        v-model="form.date"
                        type="date"
                        placeholder="选择日期"
                        :picker-options="pickerOption"
                        format="yyyy-MM-dd"
                        value-format="yyyy-MM-dd"
                    />
                </el-form-item>
                <el-form-item label="预约时间" prop="time">
                    <div style="display: flex;flex-wrap: wrap;">
                        <div style="width: 40%;margin-bottom: 10px;">
                            <el-radio v-model="form.time" border label="09:00:00">09:00-10:00</el-radio>
                        </div>
                        <div style="width: 40%;margin-bottom: 10px;">
                            <el-radio v-model="form.time" border label="10:00:00">10:00-11:00</el-radio>
                        </div>
                        <div style="width: 40%;">
                            <el-radio v-model="form.time" border label="15:10:00">15:10-16:10</el-radio>
                        </div>
                        <div style="width: 40%;">
                            <el-radio v-model="form.time" border label="16:10:00">16:10-17:10</el-radio>
                        </div>
                    </div>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { scienceRecords, scienceAdd, scienceEdit, scienceDel } from "@/api/login"

export default {
    name: "Menu",
    data() {
        return {
            total: 0,
            // 遮罩层
            loading: true,
            // 显示搜索条件
            showSearch: true,
            // 菜单表格树数据
            menuList: [],
            // 菜单树选项
            menuOptions: [],
            // 弹出层标题
            title: "新增",
            // 是否显示弹出层
            open: false,
            // 是否展开，默认全部折叠
            isExpandAll: false,
            // 重新渲染表格状态
            refreshTable: true,
            // 查询参数
            queryParams: {
                name: '',
                pageNum: 1,
                pageSize: 10,
            },
            pickerOption: {
                disabledDate(time) {
                    return time.getTime() < (Date.now() - 100.64e6);
                }
            },
            form: {// 表单参数
                name: '',       //预约人
                orderNum: 20,   //人数
                date: '',   //预约日期
                time: '', //具体时间
                phoneNum: '',   //手机号
                unit: '', //单位
                wxId: '1'
            },
            rules: {
                name: [
                    { required: true, message: "预约人姓名不能为空", trigger: "blur" }
                ],
                orderNum: [
                    { required: true, message: "预约人数不能为空", trigger: "blur" }
                ],
                date: [
                    { required: true, message: "预约日期不能为空", trigger: "blur" }
                ],
                phoneNum: [
                    { required: true, message: "联系方式不能为空", trigger: "blur" }
                ],
                unit: [
                    { required: true, message: "预约单位不能为空", trigger: "blur" }
                ],
                time: [
                    { required: true, message: "预约时间不能为空", trigger: "blur" }
                ],
            }
        }
    },
    created() {
        this.getList()
    },
    methods: {
        /** 查询菜单列表 */
        getList() {
            this.loading = true
            scienceRecords(this.queryParams).then(response => {
                this.menuList = response.data.pageData;
                this.loading = false
                this.total= response.data.totalCount
            })
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.getList()
        },
        /** 修改按钮操作 */
        handleAdd() {
            this.open = true
            this.title = "新增"
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm")
            this.handleQuery()
        },
        // 取消按钮
        cancel() {
            this.open = false
            this.reset()
        },
        // 表单重置
        reset() {
            this.form = {
                name: '',       //预约人
                orderNum: 20,   //人数
                date: '',   //预约日期
                time: '', //具体时间
                phoneNum: '',   //手机号
                unit: '', //单位
                wxId: '1'
            }
            this.resetForm("form")
        },
    
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset()
            this.form = row;
            // this.form.date= row.orderTime.split(' ')[0];
            // this.form.time= row.orderTime.split(' ')[1];
            this.$set(this.form,'date',row.orderTime.split(' ')[0]);
            this.$set(this.form,'time',row.orderTime.split(' ')[1]);
            console.log(this.form)
            this.open = true;
            this.title = "修改部门";
            this.resetForm("form");
        },
        /** 提交按钮 */
        submitForm: function() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    this.form.orderTime= this.form.date + ' ' + this.form.time;
                    if (this.form.id != undefined) {
                        scienceEdit(this.form).then(res => {
                            if(res.code=== 200) {
                                this.$modal.msgSuccess("修改成功")
                                this.open = false;
                                this.getList();
                            }  else {
                                this.$modal.msgSuccess("修改失败，请检查");
                            }
                        })
                    } else {
                        scienceAdd(this.form).then(res => {
                            if(res.code=== 200) {
                                this.$modal.msgSuccess("新增成功");
                                this.open = false;
                                this.getList();
                            } else {
                                this.$modal.msgSuccess("新增失败，请检查");
                            }
                        })
                    }
                }
            })
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            this.$modal.confirm('是否确认删除？').then(function() {
                return scienceDel({ids:row.id})
            }).then((res) => {
                console.log(res)
                if(res.code=== 200) {
                    this.getList()
                    this.$modal.msgSuccess("删除成功")
                } else [
                    this.$modal.msgSuccess("删除失败")
                ]
            }).catch(() => {})
        }
    }
}
</script>